import * as echarts from 'echarts';

// 通用图表初始化函数
function initChart(dom, chartConfig) {
    const myChart = echarts.init(dom);
    myChart.setOption(chartConfig);
    return myChart;
}

// 图表 1 的配置
export function initChart1(dom) {
    const json = {
        chart0: {
            xcategory: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'],
            low: [3, 10, 5, 20, 8, 35, 5, 25, 20, 37, 30, 39],
            lowLine: [],
        }
    };

    const zrUtil = echarts.util;

    zrUtil.each(json.chart0.xcategory, function(item, index) {
        json.chart0.lowLine.push([{
            coord: [index, json.chart0.low[index]]
        }, {
            coord: [index + 1, json.chart0.low[index + 1]]
        }]);
    });
    // const chart1Config

    const chart1Config = {
        backgroundColor: "#fff",
        grid: {
            left: '2%', // 左边距
            right: '2%', // 右边距
            top: '10%', // 上边距
            bottom: '5%', // 下边距
            containLabel: true
        },
        tooltip: { // 配置提示框
            trigger: 'axis', // 触发类型，'axis' 表示以坐标轴触发
            axisPointer: { // 坐标轴指示器
                type: 'line', // 指示器类型，'line' 表示直线
                lineStyle: { // 直线样式
                    color: '#7c1fa2', // 直线颜色
                    width: 1, // 直线宽度
                    type: 'solid' // 直线类型，实线
                }
            },
            formatter: function (params) { // 自定义提示框内容
                let result = `<div style="color:#7c1fa2; font-size:14px; font-weight:bold;">${params[0].name}</div>`;
                params.forEach(function (item) {
                    result += `<div style="color:#7c1fa2; font-size:12px;">数据：${item.value}</div>`;
                });
                return result;
            }
        },
        xAxis: {
            axisLine: {
                show: true
            },
            axisTick: {
                show: true
            },
            axisLabel: {
                interval: 0,
                show: true
            },
            data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
        },
        yAxis: {
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false
            },
        },
        series: [{
            type: 'line',
            // smooth: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: {
                normal: {
                    width: 4,
                    shadowColor: 'rgba(155, 18, 184, .3)',
                    shadowBlur: 10,
                    shadowOffsetY: 20,
                    shadowOffsetX: 20,
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 1, 0,
                        [{
                            offset: 0,
                            color: 'rgba(255, 255, 255,1)'
                        }, {
                            offset: 0.1,
                            color: 'rgba(255, 75, 172,1)'
                        }, {
                            offset: 0.9,
                            color: 'rgba(155, 18, 184,1)'
                        }, {
                            offset: 1,
                            color: 'rgba(255, 255, 255,1)'
                        }, ]
                    )
                }
            },
            itemStyle: {
                color: '#fff',
                borderColor: "#7c1fa2",
                borderWidth: 4,
            },

            data: [3, 10, 5, 20, 8, 35, 5, 25, 20, 37, 30, 39]
        },
            {
                name: '',
                type: 'lines',
                coordinateSystem: 'cartesian2d',
                zlevel: 1,
                smooth: true,
                symbol: 'circle',
                symbolSize: 25,
                effect: {
                    show: true,
                    smooth: false,
                    period: 2,
                    symbolSize: 8
                },
                lineStyle: {
                    normal: {
                        color: '#f00',
                        width: 0,
                        opacity: 0,
                        curveness: 0,
                    }
                },
                data: json.chart0.lowLine
            },

        ]
    };
    return initChart(dom, chart1Config);
}

// 图表 2 的配置
export function initChart2(dom) {
    const chart2Config = {
        backgroundColor: "#f0f0f0",
        grid: {
            left: '5%',
            right: '5%',
            top: '15%',
            bottom: '10%',
            containLabel: true
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: 'Sales',
                type: 'bar',
                data: [500, 200, 360, 1000, 900, 300, 550],
                itemStyle: {
                    color: '#3398DB'
                }
            }
        ]
    };
    return initChart(dom, chart2Config);
}

// 图表 3 的配置
export function initChart3(dom) {
    const chart3Config = {
        title: {
            text: '饼图示例'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '访问来源',
                type: 'pie',
                radius: '50%',
                data: [
                    { value: 335, name: '直接访问' },
                    { value: 310, name: '邮件营销' },
                    { value: 234, name: '联盟广告' },
                    { value: 135, name: '视频广告' },
                    { value: 1548, name: '搜索引擎' }
                ],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    return initChart(dom, chart3Config);
}